<?php
class AdminQueries {
	static function insertHomePage($homePageHTML) {
		$query = sprintf("Insert Into `Home` (`homepage_html`) Values ('%s');",
						 mysql_real_escape_string(htmlentities($homePageHTML, ENT_QUOTES)));
		
		$result = mysql_query($query);
		
		if($result) {
			$inserted = true;
		}
		else {
			$inserted = false;
			echo mysql_error();
		}
		
		return $inserted;
	}
	
	static function updateHomePage($homePageID, $homePageHTML) {
		$query = sprintf("Update `Home` set `homepage_html` = '%s' Where `ID` = %d;",
						 mysql_real_escape_string(htmlentities($homePageHTML, ENT_QUOTES)),
						 mysql_real_escape_string($homePageID));
		
		$result = mysql_query($query);
		
		if($result) {
			$updated = true;
		}
		else {
			$updated= false;
			echo mysql_error();
		}
		
		return $updated;
	}
	
	static function getAllHomePageEntries() {
		$homePages = array();
		$query = "Select `id`, `homepage_html`, `insert_tstamp` From `Home` order by id desc;";
		
		$result = mysql_query($query);
		
		if($result) {
			while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$homePages[] = new HomePage($row['id'], html_entity_decode(stripslashes($row['homepage_html'])), $row['insert_tstamp']);
			}
		}
		else {
			echo mysql_error();
		}
		
		return $homePages;
	}
	
	static function getHomePageEntry($homePageID) {
		$query = sprintf("Select `ID`, `homepage_html`, `insert_tstamp` From `Home` Where `id` = %d;",
						 mysql_real_escape_string($homePageID));
		
		$result = mysql_query($query);
		
		if($result) {
			$row = mysql_fetch_array($result, MYSQL_ASSOC);
			$homePage = new HomePage($row['ID'], html_entity_decode(stripslashes($row['homepage_html'])), $row['insert_tstamp']);
		}
		else {
			echo mysql_error();
		}
		
		return $homePage;
	}
}
?>